home *** CD-ROM | disk | FTP | other *** search
-
-
- /****************************************************************************
- * PHE2TXT.C - version 1.0
- *
- * This module implements the POV-Help database to Plain ASCII Text converter.
- *
- * from Persistence of Vision Raytracer
- * Copyright 1994 Persistence of Vision Team
- * Copyright 1994 Christopher J. Cason.
- *---------------------------------------------------------------------------
- * NOTICE: This source code file is provided so that users may experiment
- * with enhancements to POV-Ray and to port the software to platforms other
- * than those supported by the POV-Ray Team. There are strict rules under
- * which you are permitted to use this file. The rules are in the file
- * named POVLEGAL.DOC which should be distributed with this file. If
- * POVLEGAL.DOC is not available or for more info please contact the POV-Ray
- * Team Coordinator by leaving a message in CompuServe's Graphics Developer's
- * Forum. The latest version of POV-Ray may be found there as well.
- *
- * POV-Ray files may also be obtained from ftp.uwa.edu.au in pub/povray.
- *
- * This program was written in its entirety by Christopher J. Cason.
- * Its use is freely and permanently granted to the POV-Team and POV users
- * under the conditions specified in POVLEGAL.DOC.
- *
- * Author : C. J. Cason (cjcason@yarrow.wt.uwa.edu.au, CIS 100032,1644)
- *
- *****************************************************************************/
-
- /*---------------------------------------------------------------------------*/
- /* NOTE : If you're porting this to another architecture, don't forget that */
- /* the POV-Help database stores words and dwords in LSB-first format. */
- /*---------------------------------------------------------------------------*/
-
- #include <stdio.h>
- #include <mem.h>
- #include <string.h>
- #include <alloc.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <dos.h>
- #include <bios.h>
- #include <ctype.h>
- #include "help.h"
-
- typedef unsigned char uchar ;
-
- #define MAX_REF 512
- #define MAX_FRAGMENT 16
- #define MAX_LINK 64
- #define VSIZE_Y 128
- #define VSIZE_X 128
-
- #define TITLE 0x01
- #define COPYRIGHT 0x02
- #define AUTHOR 0x03
- #define FAQ 0x04
- #define CONTENTS 0x100
-
- #define PRESERVE (preserve_formatting || code_fragment || line_drawing)
-
- #ifdef max
- #undef max
- #endif
-
- #ifdef min
- #undef min
- #endif
-
- #define max(a,b) ((int) (a) > (int) (b) ? (a) : (b))
- #define min(a,b) ((int) (a) < (int) (b) ? (a) : (b))
-
- typedef struct
- {
- unsigned char start_vx ;
- unsigned char end_vx ;
- unsigned short id ;
- unsigned short start_vy ;
- unsigned short end_vy ;
- } viewer_reference ;
-
- typedef struct
- {
- unsigned char start_vx ;
- unsigned char end_vx ;
- unsigned short start_vy ;
- unsigned short end_vy ;
- } viewer_code_fragment ;
-
- typedef struct
- {
- /* these first three entries must not be changed/moved */
- unsigned long section ;
- unsigned long section_length ;
- unsigned char is_appendix ;
- unsigned char number [16] ;
- unsigned char title [80] ;
- unsigned index ;
- } viewer_toc ;
-
- uchar *title ;
- uchar blank [VSIZE_X] ;
- uchar *reference_strings ;
- uchar **reference_string_index ;
- void far *pageframe ;
- short first_line = 0 ;
- short wsize_y = 15 ;
- unsigned char attribute ;
- unsigned char reference ;
- unsigned char highlight ;
- unsigned char bold ;
- unsigned char code_fragment ;
- unsigned char heading ;
- unsigned char line_drawing ;
- unsigned char preserve_formatting ;
- unsigned char table ;
- unsigned char list ;
- unsigned char list_entry ;
- unsigned char vx ;
- unsigned char wsize_x = 77 ;
- unsigned char left_margin ;
- unsigned char right_margin ;
- unsigned char (*page_buffer) [VSIZE_Y] [VSIZE_X] ;
- unsigned char (*attribute_buffer) [VSIZE_Y] [VSIZE_X] ;
- unsigned char *toc ;
- unsigned char *section ;
- unsigned long links [MAX_LINK] ;
- unsigned long *reference_index ;
- unsigned short vy ;
- unsigned short reference_count ;
- unsigned short code_fragment_count ;
- unsigned short TOCsize ;
- unsigned short ignore_lines = 0 ;
- unsigned short link_count ;
- unsigned short target_line ;
- unsigned short noFAQ = 1 ;
- unsigned short justifyOn = 1 ;
- FILE *inF ;
- FILE *outF ;
- viewer_toc tc ;
- viewer_reference *references ;
- viewer_code_fragment *code_fragments ;
- help_file_header header ;
-
- unsigned astrlen (char *s, unsigned width)
- {
- char *s1 = s + width ;
-
- while (*--s1 == ' ' && s1 > s) ;
- return ((unsigned) (s1 - s)) ;
- }
-
- unsigned wordcount (char *s, unsigned width)
- {
- unsigned wc = 0 ;
- unsigned inword = 0 ;
-
- while (width--)
- {
- if (inword == 0)
- {
- if (*s++ == ' ') continue ;
- inword++ ;
- wc++ ;
- }
- else
- {
- if (*s++ != ' ') continue ;
- inword = 0 ;
- }
- }
- return (wc) ;
- }
-
- void insertspaces (unsigned howmany, char *s, unsigned width)
- {
- char *s1 = s + width ;
-
- while (howmany--)
- {
- while (*--s1 == ' ' && s1 > s) ;
- if (s1 == s) return ;
- while (*--s1 != ' ' && s1 > s) ;
- if (s1 == s) return ;
- memmove (s1 + 1, s1, (width - (unsigned) (s1 - s) - 1)) ;
- }
- }
-
- void justify (char *s, unsigned width)
- {
- int needed ;
- unsigned wc ;
-
- if ((needed = width - astrlen (s, width) - 1) <= 0) return ;
- if ((wc = wordcount (s, width)) == 0) return ;
- while (needed > wc)
- {
- insertspaces (needed, s, width) ;
- needed -= wc ;
- }
- insertspaces (needed, s, width) ;
- }
-
- unsigned ustrcmp (char *s1, char *s2)
- {
- while (*s1 && *s2)
- if (toupper (*s1++) != toupper (*s2++))
- return (1) ;
- return (*s1 || *s2) ;
- }
-
- unsigned page_buffer_full (void)
- {
- return (vy >= VSIZE_Y - 1) ;
- }
-
- void clear_page_buffer (void)
- {
- vx = 0 ;
- vy = 0 ;
- left_margin = 0 ;
- right_margin = wsize_x ;
- memset (page_buffer, ' ', VSIZE_X * VSIZE_Y) ;
- memset (attribute_buffer, 0, VSIZE_X * VSIZE_Y) ;
- }
-
- unsigned line_is_blank (unsigned line)
- {
- uchar *s = (*page_buffer) [line] ;
- unsigned count = VSIZE_X ;
-
- while (count--)
- if (*--s != ' ')
- return (0) ;
- return (1) ;
- }
-
- char *trim (char *line)
- {
- char *s ;
-
- s = line + VSIZE_X - 1 ;
- while (s >= line && *s == ' ') s-- ;
- *++s = '\0' ;
- return (line) ;
- }
-
- /* in device_setup, do whatever setup is needed for your output medium */
-
- void device_setup (void)
- {
- }
-
- unsigned process_page (void)
- {
- int i ;
-
- for (i = 0 ; i <= vy ; i++)
- fprintf (outF, "%.*s\n", wsize_x, trim ((*page_buffer) [i])) ;
- clear_page_buffer () ;
- return (0) ;
- }
-
- void process_section (void)
- {
- fprintf (outF, "\n") ;
- }
-
- void write_attribute (int x1, int y1, int x2, int y2, char attr)
- {
- unsigned count = (y2 * VSIZE_X + x2) - (y1 * VSIZE_X + x1) ;
-
- if (y2 < y1) return ;
- if (y2 == y1 && x2 < x1) return ;
- memset (attribute_buffer [y1] + x1, attr, count) ;
- }
-
- void clear_attributes (void)
- {
- reference = highlight = bold = code_fragment = 0 ;
- heading = line_drawing = preserve_formatting = 0 ;
- table = list = list_entry = 0 ;
- }
-
- void set_attribute (void)
- {
- attribute = 0 ;
- if (code_fragment) attribute |= CODE_FRAGMENT ;
- if (heading) attribute |= HEADING ;
- if (line_drawing) attribute |= LINE_DRAWING ;
- if (list_entry) attribute |= LIST_ENTRY ;
- if (bold) attribute |= BOLD ;
- if (highlight) attribute |= HIGHLIGHT ;
- if (reference) attribute |= REF ;
- }
-
- viewer_reference *create_reference (unsigned short id)
- {
- if (reference_count == MAX_REF) return (NULL) ;
- references [reference_count].id = id ;
- return (references + reference_count++) ;
- }
-
- void clear_references (void)
- {
- reference_count = 0 ;
- }
-
- viewer_code_fragment *create_code_fragment (void)
- {
- if (code_fragment_count == MAX_FRAGMENT) return (NULL) ;
- return (code_fragments + code_fragment_count++) ;
- }
-
- void clear_code_fragments (void)
- {
- code_fragment_count = 0 ;
- }
-
- viewer_toc *getTC (unsigned index)
- {
- uchar *s ;
- static viewer_toc result ;
-
- if (index >= header.section_count + header.appendix_count) index = 0 ;
- s = toc + (unsigned) ((unsigned long) TOCsize * index) ;
- memcpy (&result, s, sizeof (TOC_entry)) ;
- s += sizeof (TOC_entry) ;
- strcpy (result.number, s) ;
- strcpy (result.title, s + header.sec_number_len) ;
- result.index = index ;
- return (&result) ;
- }
-
- viewer_toc *findTCfromOffset (unsigned long offset)
- {
- unsigned i ;
- static viewer_toc result ;
-
- for (i = 0 ; i < header.section_count + header.appendix_count ; i++)
- {
- result = *getTC (i) ;
- if (result.section > offset) continue ;
- if (result.section + result.section_length <= offset) continue ;
- return (&result) ;
- }
- return (NULL) ;
- }
-
- char *get_section (viewer_toc *tc, unsigned number)
- {
- uchar *section ;
- unsigned count ;
-
- *tc = *getTC (number) ;
- fseek (inF, tc->section, SEEK_SET) ;
- if (tc->section_length + 1 > 65534U)
- {
- printf ("section too big for present allocation code") ;
- return (NULL) ;
- }
- if ((section = calloc ((unsigned) tc->section_length + 1, 1)) == NULL)
- {
- printf ("error - cannot allocate enough memory for section %s", tc->number) ;
- return (NULL) ;
- }
- if (fread (section, (unsigned) tc->section_length + 1, 1, inF) == 0)
- {
- printf ("could not read section") ;
- return (NULL) ;
- }
- count = (unsigned) tc->section_length ;
- while (section [count - 1] == '\n' || section [count - 1] == PARAGRAPH) count-- ;
- section [count] = '\0' ;
- return (section) ;
- }
-
- unsigned findSection (char *s)
- {
- unsigned i ;
- viewer_toc result ;
-
- for (i = 0 ; i < header.section_count + header.appendix_count ; i++)
- {
- result = *getTC (i) ;
- if (ustrcmp (s, result.number)) continue ;
- return (i) ;
- }
- return (-1) ;
- }
-
- unsigned findTitle (char *s)
- {
- static char str [80] ;
- unsigned i ;
- viewer_toc result ;
-
- strupr (s) ;
- for (i = 0 ; i < header.section_count + header.appendix_count ; i++)
- {
- result = *getTC (i) ;
- if (ustrcmp (s, result.title)) continue ;
- return (i) ;
- }
- for (i = 0 ; i < header.section_count + header.appendix_count ; i++)
- {
- result = *getTC (i) ;
- strcpy (str, result.title) ;
- strupr (str) ;
- if (strstr (str, s) == NULL) continue ;
- return (i) ;
- }
- return (-1) ;
- }
-
- void vputchar (unsigned char ch)
- {
- if (vy >= VSIZE_Y) return ;
-
- switch (ch)
- {
- case '\r' :
- vx = 0 ;
- return ;
-
- case '\n' :
- vx = VSIZE_X ;
- break ;
-
- default :
- if (vx >= VSIZE_X) return ;
- if (ignore_lines) break ;
- (*page_buffer) [vy] [vx] = ch ;
- (*attribute_buffer) [vy] [vx] = attribute ;
- break ;
- }
-
- if (++vx >= VSIZE_X)
- {
- if (ch == '\n' || !PRESERVE)
- {
- vx = left_margin ;
- if (ignore_lines)
- {
- --ignore_lines ;
- return ;
- }
- if (++vy >= VSIZE_Y) vy = VSIZE_Y ;
- }
- }
- }
-
- void vputchar_attr (unsigned char ch, unsigned char attr)
- {
- unsigned char a = attribute ;
-
- attribute = attr ;
- vputchar (ch) ;
- attribute = a ;
- }
-
- void vputstr (char *s)
- {
- while (*s)
- vputchar (*s++) ;
- }
-
- void cvputstr (char *s)
- {
- static char str [256] ;
- char *s1 = str ;
- unsigned count ;
-
- for (count = 0 ;; s++)
- {
- if (page_buffer_full ())
- if (process_page ())
- break ;
-
- if (*s == '\n' || *s == '\0')
- {
- vx = (wsize_x - count) / 2 ;
- if (vx < 0) vx = 0 ;
- for (s1 = str ; count ; count--)
- vputchar (*s1++) ;
- vputchar ('\n') ;
- if (*s == '\0') break ;
- s1 = str ;
- continue ;
- }
- *s1++ = *s ;
- count++ ;
- }
- }
-
- unsigned pointcount (char *s)
- {
- unsigned count = 0 ;
-
- while (*s)
- if (*s++ == '.')
- count++ ;
- return (count) ;
- }
-
- void vcachechar (char ch)
- {
- char *s ;
- static char str [128] ;
- static unsigned count = 0 ;
-
- if (ch == '\0' || ch == ' ' || ch == '\t' || ch == '\n' || count == 128)
- {
- if (count / 2 > right_margin - vx)
- {
- if (justifyOn)
- justify ((char *) ((*page_buffer) [vy]), wsize_x) ;
- vputchar ('\n') ;
- }
- for (s = str ; count ; count -= 2, s += 2)
- if (*s != ' ' || vx != left_margin)
- vputchar_attr (s [0], s [1]) ;
- if (ch == '\0') return ;
- }
- if (PRESERVE)
- {
- vputchar (ch) ;
- return ;
- }
- str [count++] = ch ;
- str [count++] = attribute ;
- }
-
- void format_section (viewer_toc *tc, unsigned char *section)
- {
- unsigned id ;
- unsigned escape = 0 ;
- unsigned char *s ;
- viewer_reference *r ;
- viewer_code_fragment *f ;
-
- clear_attributes () ;
- set_attribute () ;
- clear_references () ;
- clear_code_fragments () ;
- clear_page_buffer () ;
- ignore_lines = first_line ;
- target_line = 0 ;
- if (tc)
- {
- heading++ ;
- set_attribute () ;
- if (tc->is_appendix) vputstr ("APPENDIX ") ;
- vputstr (tc->number) ;
- vx = 17 ;
- vputstr (tc->title) ;
- heading = 0 ;
- vcachechar ('\n') ;
- vcachechar ('\n') ;
- set_attribute () ;
- }
-
- while (*section == '\n' || *section == PARAGRAPH) section++ ;
-
- for (s = section ; *s ; s++)
- {
- if (page_buffer_full ())
- if (process_page ())
- break ;
-
- if (escape == 0)
- {
- switch (*s)
- {
- case ESCAPE :
- escape++ ;
- break ;
-
- case PARAGRAPH :
- vcachechar ('\n') ;
- vcachechar ('\n') ;
- break ;
-
- case INDENT :
- vx = 3 ;
- break ;
-
- case REFERENCE :
- id = *++s ;
- id += (unsigned) *++s * 256 ;
- r = create_reference (id) ;
- break ;
-
- case TARGET :
- break ;
-
- case REFERENCE_ON :
- if (r == NULL) break ;
- vcachechar ('\0') ;
- r->start_vx = vx ;
- r->start_vy = vy ;
- reference++ ;
- set_attribute () ;
- break ;
-
- case REFERENCE_OFF :
- if (r == NULL) break ;
- vcachechar ('\0') ;
- r->end_vx = vx ;
- r->end_vy = vy ;
- reference = 0 ;
- r = NULL ;
- set_attribute () ;
- break ;
-
- case HIGHLIGHT_ON :
- case HIGHLIGHT_OFF :
- highlight = *s == HIGHLIGHT_ON ;
- set_attribute () ;
- break ;
-
- case BOLD_ON :
- case BOLD_OFF :
- bold = *s == BOLD_ON ;
- set_attribute () ;
- break ;
-
- case CODE_ON :
- code_fragment++ ;
- set_attribute () ;
- vcachechar ('\0') ;
- if ((f = create_code_fragment ()) == NULL) break ;
- f->start_vx = vx ;
- f->start_vy = vy ;
- break ;
-
- case CODE_OFF :
- code_fragment = 0 ;
- set_attribute () ;
- vcachechar ('\0') ;
- if (f == NULL) break ;
- f->end_vx = vx ;
- f->end_vy = vy ;
- f = NULL ;
- break ;
-
- case HEADING_ON :
- case HEADING_OFF :
- heading = *s == HEADING_ON ;
- set_attribute () ;
- break ;
-
- case LINE_ON :
- case LINE_OFF :
- line_drawing = *s == LINE_ON ;
- set_attribute () ;
- vcachechar ('\0') ;
- break ;
-
- case PRESERVE_ON :
- case PRESERVE_OFF :
- preserve_formatting = *s == PRESERVE_ON ;
- set_attribute () ;
- vcachechar ('\0') ;
- break ;
-
- case TABLE_ON :
- case TABLE_OFF :
- table = *s == TABLE_ON ;
- set_attribute () ;
- break ;
-
- case LIST_ON :
- if ((list = *++s) == 1) list++ ;
- left_margin = list += 2 ;
- break ;
-
- case LIST_OFF :
- left_margin = list = 0 ;
- break ;
-
- case LIST_ENTRY_ON :
- list_entry++ ;
- vcachechar ('\0') ;
- set_attribute () ;
- vx = 2 ;
- break ;
-
- case LIST_ENTRY_OFF :
- list_entry = 0 ;
- vcachechar ('\0') ;
- set_attribute () ;
- vx = left_margin ;
- while (*s == ' ') s++ ;
- break ;
-
- default :
- vcachechar (*s) ;
- break ;
- }
- }
- else
- {
- vcachechar (*s) ;
- escape = 0 ;
- }
- }
- vcachechar ('\0') ;
- while (vy > 0 && line_is_blank (vy)) vy-- ;
- if (reference)
- {
- reference_count-- ;
- reference = 0 ;
- }
- process_page () ;
- process_section () ;
- }
-
- void format_authors (char *s)
- {
- clear_attributes () ;
- set_attribute () ;
- clear_references () ;
- clear_page_buffer () ;
- ignore_lines = first_line ;
- target_line = 0 ;
-
- cvputstr ("AUTHORS\r\n") ;
-
- cvputstr (s) ;
- while (vy > 0 && line_is_blank (vy)) vy-- ;
- }
-
- void load_links (void)
- {
- unsigned child_count ;
-
- link_count = 0 ;
- fseek (inF, reference_index [references [0].id], SEEK_SET) ;
- fread (&child_count, 2, 1, inF) ;
- if (child_count >= MAX_LINK - 1) child_count = MAX_LINK - 1 ;
- fread (links, 4, link_count = child_count + 1, inF) ;
- }
-
- void display_text (unsigned what)
- {
- char *text ;
- char *s ;
- unsigned i ;
- unsigned long offset ;
- unsigned long length ;
-
- switch (what)
- {
- case COPYRIGHT :
- offset = header.copyright ;
- length = header.copyright_length ;
- break ;
-
- case AUTHOR :
- offset = header.authors ;
- length = header.author_length ;
- break ;
-
- default :
- return ;
- }
-
- first_line = 0 ;
-
- if (length > 65534U)
- {
- printf ("section too big for present allocation code\r\n") ;
- return ;
- }
- if ((text = calloc ((unsigned) length + 1, 1)) == NULL)
- {
- printf ("cannot allocate memory for text\r\n") ;
- return ;
- }
- fseek (inF, offset, SEEK_SET) ;
- if (fread (text, (unsigned) length + 1, 1, inF) == 0)
- {
- printf ("could not read text\r\n") ;
- free (text) ;
- return ;
- }
- text [(unsigned) length] = 0 ;
-
- for (s = text, i = (unsigned) length ; i ; i--, s++)
- if (*s == '\0')
- *s = '\n' ;
-
- if (what == AUTHOR)
- format_authors (text) ;
- else
- format_section (NULL, text) ;
-
- free (text) ;
- process_page () ;
- process_section () ;
- }
-
- void display_contents (void)
- {
- unsigned i ;
- unsigned offset ;
- viewer_toc tc ;
-
- clear_attributes () ;
- set_attribute () ;
- clear_references () ;
- clear_page_buffer () ;
-
- vputstr ("\r\n") ;
- attribute = 0 ;
- cvputstr (title) ;
- vputstr ("\r\n") ;
-
- for (i = 0 ; i < header.section_count + header.appendix_count ; i++)
- {
- if (page_buffer_full ())
- if (process_page ())
- break ;
-
- if (i == header.section_count)
- {
- vputstr ("\r\n") ;
- cvputstr ("*** APPENDICES ***") ;
- vputstr ("\r\n") ;
- }
- tc = *getTC (i) ;
- if (noFAQ && strstr (tc.title, "FREQUENTLY ASKED QUESTIONS") != NULL) break ;
- offset = pointcount (tc.number) * 3 ;
- vx = offset ;
- vputstr (tc.number) ;
- vx = 17 + offset ;
- vputstr (tc.title) ;
- vputstr ("\r\n") ;
- }
- while (vy > 0 && line_is_blank (vy)) vy-- ;
- process_page () ;
- process_section () ;
- }
-
- unsigned load_strings (void)
- {
- char *s ;
- char str [81] ;
- unsigned i ;
- unsigned count ;
-
- if ((reference_strings = farcalloc ((unsigned) header.reference_string_length, 1)) == NULL) return (1) ;
- if ((reference_string_index = farcalloc (header.reference_count * 4, 1)) == NULL) return (1) ;
- s = reference_strings ;
- for (i = 0 ; i < header.reference_count ; i++)
- {
- fseek (inF, reference_index [i], SEEK_SET) ;
- fread (&count, 2, 1, inF) ;
- fseek (inF, (count + 1) * 4, SEEK_CUR) ;
- if (fgets (str, 80, inF) == NULL) break ;
- strcpy (s, str) ;
- reference_string_index [i] = s ;
- s += strlen (str) + 1 ;
- }
- return (0) ;
- }
-
- void init (void)
- {
- if ((references = calloc (sizeof (viewer_reference), MAX_REF)) == NULL)
- {
- printf ("cannot allocate memory for references\r\n") ;
- exit (1) ;
- }
- if ((code_fragments = calloc (sizeof (viewer_code_fragment), MAX_FRAGMENT)) == NULL)
- {
- printf ("cannot allocate memory for code fragments\r\n") ;
- exit (1) ;
- }
- if ((page_buffer = calloc (VSIZE_X, VSIZE_Y)) == NULL)
- {
- printf ("cannot allocate memory for page buffer\r\n") ;
- exit (1) ;
- }
- if ((attribute_buffer = calloc (VSIZE_X, VSIZE_Y)) == NULL)
- {
- printf ("cannot allocate memory for attribute buffer\r\n") ;
- exit (1) ;
- }
- clear_page_buffer () ;
- memcpy (blank, page_buffer, VSIZE_X) ;
- if ((toc = malloc ((unsigned) header.table_of_contents_length)) == NULL)
- {
- printf ("cannot allocate memory for table of contents\r\n") ;
- exit (1) ;
- }
- TOCsize = sizeof (TOC_entry) + header.sec_number_len + header.sec_title_len ;
- if ((reference_index = malloc ((unsigned) header.reference_index_length)) == NULL)
- {
- printf ("cannot allocate memory for reference index\r\n") ;
- exit (1) ;
- }
- fseek (inF, header.reference_index, SEEK_SET) ;
- if (fread (reference_index, (unsigned) header.reference_index_length, 1, inF) == 0)
- {
- printf ("could not read reference index\r\n") ;
- exit (1) ;
- }
- if ((title = calloc ((unsigned) header.title_length + 1, 1)) == NULL)
- {
- printf ("cannot allocate memory for title\r\n") ;
- exit (1) ;
- }
- fseek (inF, header.title, SEEK_SET) ;
- if (fread (title, (unsigned) header.title_length + 1, 1, inF) == 0)
- {
- printf ("could not read title\r\n") ;
- exit (1) ;
- }
- title [(unsigned) header.title_length] = 0 ;
- if (load_strings ())
- {
- printf ("could not load reference strings\r\n") ;
- exit (1) ;
- }
- }
-
- unsigned main (unsigned argc, char *argv [])
- {
- int number = -1 ;
- int count = -1 ;
- char *s1 = "HELP.PHE" ;
- char *s2 = "" ;
- char gotoSection [16] = "1.0" ;
- char gotoTitle [80] = "" ;
-
- while (++argv, --argc)
- {
- if (**argv == '-' || **argv == '+')
- {
- switch (toupper ((*argv) [1]))
- {
- case 'I' :
- s1 = *argv + 2 ;
- break ;
-
- case 'O' :
- s2 = *argv + 2 ;
- break ;
-
- case 'W' :
- wsize_x = min (75, atoi (*argv + 2)) ;
- break ;
-
- case 'F' :
- noFAQ = 0 ;
- break ;
-
- case 'N' :
- number = atoi (*argv + 2) ;
- if (count == -1) count = 0 ;
- break ;
-
- case 'S' :
- strncpy (gotoSection, *argv + 2, 15) ;
- if (count == -1) count = 0 ;
- break ;
-
- case 'T' :
- strncpy (gotoTitle, *argv + 2, 79) ;
- if (count == -1) count = 0 ;
- break ;
-
- case 'C' :
- count = atoi (*argv + 2) ;
- break ;
-
- case 'J' :
- justifyOn = (*argv) [2] != '-' ;
- break ;
-
- case '?' :
- case 'H' :
- printf ("\r\nPHE2TXT : POV-Help to Text\r\n\n") ;
- printf ("-i : set input file name (default HELP.PHE)\r\n") ;
- printf ("-o : set output file name (default stdout)\r\n") ;
- printf ("-w : set width for output (default 77 characters)\r\n") ;
- printf ("-j[-] : justify ON (default), -j- to turn off\r\n") ;
- printf ("-f : include FAQ section (if present) in output (default omit)\r\n") ;
- printf ("-sX.Y : start from section X.Y (default 1.0)\r\n") ;
- printf ("-tABC : start from section with title ABC\r\n") ;
- printf ("-nn : start from the nth section (first = 0)\r\n") ;
- printf ("-cn : process 'n' sections (default all unless -s, -t or -n, where 'n' = 1)\r\n") ;
- printf ("\r\n") ;
- return (0) ;
-
- default :
- printf ("unknown option '%s'\r\n", *argv) ;
- return (1) ;
- }
- }
- }
-
- if ((inF = fopen (s1, "rb")) == NULL)
- {
- printf ("could not open input file '%s'\r\n", s1) ;
- printf ("use phe2txt -h for help\r\n") ;
- return (1) ;
- }
- if (*s2)
- {
- if ((outF = fopen (s2, "wt")) == NULL)
- {
- printf ("could not open output file '%s'\r\n", s2) ;
- printf ("use phe2txt -h for help\r\n") ;
- return (1) ;
- }
- }
- else
- outF = stdout ;
- if (fread (&header, sizeof (help_file_header), 1, inF) == 0)
- {
- printf ("could not read header\r\n") ;
- return (1) ;
- }
-
- if (memcmp (header.signature, "POV-Help", 8) != 0)
- {
- printf ("header ID failed consistency check\r\n") ;
- return (1) ;
- }
-
- if (header.reader_version > VERSION)
- {
- printf ("version error ! database is not compatible [needs version %d.%d of reader]\r\n",
- header.reader_version / 100, header.reader_version % 100) ;
- return (1) ;
- }
-
- init () ;
-
- fseek (inF, header.table_of_contents, SEEK_SET) ;
- if (fread (toc, (unsigned) header.table_of_contents_length, 1, inF) == 0)
- {
- printf ("could not read table of contents\r\n") ;
- return (1) ;
- }
-
- left_margin = 0 ;
- right_margin = wsize_x ;
- device_setup () ;
-
- if (*gotoTitle) number = findTitle (gotoTitle) ;
- if (*gotoSection) number = findSection (gotoSection) ;
-
- if (number == -1)
- {
- number = 0 ;
- count = header.appendix_count + header.section_count ;
- }
- else
- if (count == 0)
- count = 1 ;
-
- if (count == -1)
- count = header.appendix_count + header.section_count - number ;
-
- if (number + count > header.appendix_count + header.section_count)
- count = header.appendix_count + header.section_count - number ;
-
- display_contents () ;
-
- first_line = 0 ;
-
- while (count--)
- {
- section = get_section (&tc, number++) ;
- if (noFAQ && strstr (tc.title, "FREQUENTLY ASKED QUESTIONS") != NULL) break ;
- format_section (&tc, section) ;
- free (section) ;
- }
-
- clear_attributes () ;
- set_attribute () ;
- clear_references () ;
- clear_page_buffer () ;
-
- display_text (AUTHOR) ;
-
- farfree (reference_string_index) ;
- farfree (reference_strings) ;
- free (toc) ;
- free (page_buffer) ;
- free (attribute_buffer) ;
- free (code_fragments) ;
- free (references) ;
- free (title) ;
- fclose (inF) ;
- return (0) ;
- }